#!/usr/bin/env python3

import os
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('AyatanaAppIndicator3', '0.1')
gi.require_version('Notify', '0.7')
from gi.repository import Gtk, Gdk, GLib, AyatanaAppIndicator3


desktop = os.environ.get("XDG_CURRENT_DESKTOP", "")

if desktop == "KDE":
    APPINDICATOR_ID = ""
else:
    APPINDICATOR_ID = "Random Background"

print("APPINDICATOR_ID =", APPINDICATOR_ID)



def quit(source):
    Gtk.main_quit()

def script1(widget):
    os.popen("`sh -c /usr/share/random-background/kde/data/scripts/change`")

def script2(widget):
    os.popen("/usr/share/random-background/kde/data/chooser.py")

def script3(widget):
    os.popen("$HOME/.random-background/wallpaper_net.py")

def script4(widget):
    os.popen("/usr/share/random-background/kde/data/net/save_pic.py")

def script5(widget):
    os.popen("/usr/share/random-background/kde/data/scripts/settings")

def script6(widget):
    os.popen("/usr/share/random-background/icons/about/about.py")

def script7(widget):
    os.popen("pkill -f 'Random Background'")
    os.popen("rm -f /tmp/tmp* > /dev/null 2>&1")

def build_menu():
    menu = Gtk.Menu()

    # Add tooltips to each menu item
    item_menu = Gtk.MenuItem(label="🔰️ Change")
    item_menu.connect('activate', script1)
    item_menu.set_tooltip_text("Change Current Image")
    menu.append(item_menu)

    item_choose = Gtk.MenuItem(label="🏞️ Choose")
    item_choose.connect('activate', script2)
    item_choose.set_tooltip_text("Select A Image")
    menu.append(item_choose)

    item_save = Gtk.MenuItem(label="🌐️ Download")
    item_save.connect('activate', script3)
    item_save.set_tooltip_text("Change Download Image")
    menu.append(item_save)

    item_save = Gtk.MenuItem(label="📂️ Save")
    item_save.connect('activate', script4)
    item_save.set_tooltip_text("Save Download Image")
    menu.append(item_save)

    item_playing = Gtk.MenuItem(label="⚙️ Settings")
    item_playing.connect('activate', script5)
    item_playing.set_tooltip_text("Change Background Settings")
    menu.append(item_playing)

    item_about = Gtk.MenuItem(label="❓️ About")
    item_about.connect('activate', script6)
    item_about.set_tooltip_text("About Random Background")
    menu.append(item_about)

    item_quit = Gtk.MenuItem(label="❌️ Quit")
    item_quit.set_tooltip_text("Quit Random Background")
    item_quit.connect('activate', script7)
    menu.append(item_quit)

    menu.show_all()
    return menu

def on_status_icon_click(icon, event):
    if event.type == Gdk.EventType.BUTTON_PRESS and (event.button == 1 or event.button == 3):
        # Show the menu
        menu.popup(None, None, Gtk.StatusIcon.position_menu, icon, event.button, event.time)
        return True

# Create the indicator using libayatana-appindicator
indicator = AyatanaAppIndicator3.Indicator.new(
    APPINDICATOR_ID,
    "/usr/share/icons/hicolor/48x48/apps/random-background.png",
    AyatanaAppIndicator3.IndicatorCategory.APPLICATION_STATUS
)

# Set the menu
menu = build_menu()
indicator.set_menu(menu)

# Create a Gtk.StatusIcon for mouse events
status_icon = Gtk.StatusIcon()
status_icon.set_from_file("/usr/share/icons/hicolor/48x48/apps/random-background.png")
status_icon.set_visible(True)

# Connect the button press event
status_icon.connect('button-press-event', on_status_icon_click)

Gtk.main()

